package com.jh3502.vivo.function.travelnew;

/**
 * @author harisucici
 * @date 2017/6/19
 */

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jh3502.vivo.R;
import com.jh3502.vivo.net.ApiConstants;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PhotoChosenAdapter extends RecyclerView.Adapter<PhotoChosenAdapter.ViewHolder> implements View.OnClickListener {


    private Context mContext;
    private List<String> urls = new ArrayList<>(2);

    private View convertView;
    @Nullable
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    @Nullable
    private ButtonInterface viewClickDelete;
    private boolean show = true;

    public PhotoChosenAdapter(Context context, List<String> urls) {
        this.mContext = context;
        this.urls = urls;
    }

    public PhotoChosenAdapter(Context context) {
        this.mContext = context;

    }

    public void setButtonSetOnclickDelete(ButtonInterface buttonInterface) {
        this.viewClickDelete = buttonInterface;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_pic_choose, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        convertView.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        viewHolder.itemView.setTag(position);
        String s = ApiConstants.getApiHost() + urls.get(position);
        Glide.with(mContext).load(ApiConstants.getApiHost() + urls.get(position)).into(viewHolder.itemPicChosenPic);
        if (show) {
            viewHolder.itemPicChosenDel.setVisibility(View.VISIBLE);
        } else {
            viewHolder.itemPicChosenDel.setVisibility(View.GONE);
        }
        viewHolder.itemPicChosenDel.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickDelete != null) {
                viewClickDelete.onClick(position);

            }
        });
    }

    @Override
    public void onClick(@NonNull View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((int) v.getTag());
        }
    }


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return urls.size();
    }

    public void setShowDelete(boolean b) {
        show = b;
    }


    public interface ButtonInterface {
        void onClick(int position);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(int s);
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_pic_chosen_pic)
        ImageView itemPicChosenPic;
        @BindView(R.id.item_pic_chosen_del)
        ImageView itemPicChosenDel;

        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}